import { CUqBase } from 'uq-app';
import { Product, ReturnQueryProductByIDRet } from 'uq-app/uqs/BzTicket';
import { VProduct } from './VProduct';
import { VProductEdit } from './VProductEdit';
import { VProductNew } from './VProductNew';

export class CProduct extends CUqBase {
  newNo: string;
  productList: Product[];
  product: ReturnQueryProductByIDRet;
  status: number;

  protected async internalStart() {
    await this.loadList();
    this.openVPage(VProduct);
  }

  newProduct = async () => {
    let { BzTicket } = this.uqs;
    this.newNo = await BzTicket.IDNO({ ID: BzTicket.Product });
    this.status = 1;
    this.openVPage(VProductNew);
  }

  onNewSubmit = async (data: Product) => {
    if (!data.no) {
      alert('请输入编号');
    }
    let { BzTicket } = this.uqs;
    let ret = await BzTicket.Acts(
      {
        product: [data],
      }
    );
    await this.loadList();
    this.replaceVPage(VProduct);
  }

  editProduct = async (item: Product) => {
    let { BzTicket } = this.uqs;
    this.product = await (
      await BzTicket.QueryProductByID.query({ id: item.id })
    ).ret[0];
    this.openVPage(VProductEdit);
  }

  onEditSubmit = async (data: Product) => {
    let { BzTicket } = this.uqs;
    await BzTicket.UpdateProduct.submit({
      id: this.product.id,
      name: data.name,
      version: data.version,
      desc: data.desc,
      preNo: data.preNo,
      isbackend: data.isbackend,
      backVersionNo: data.backVersionNo,
      status: data.status,
    });
    await this.loadList();
    this.replaceVPage(VProduct);
  }

  private async loadList() {
    let { BzTicket } = this.uqs;
    let productList = await BzTicket.QueryID<Product>({
      ID: BzTicket.Product,
      page: {
        start: 0,
        size: 100,
      },
    });
    this.runInAction(() => {
      this.productList = productList;
    });
  }
}
